package com.wyb.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class IsLoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp= (HttpServletResponse) servletResponse;
        HttpSession session=req.getSession();
        Object isLogin = session.getAttribute("isLogin");
        //System.out.println(isLogin);
        //判断是否登录
        if (isLogin==null||isLogin.equals(false)){
            //如果未登录，跳转到登录页面
            resp.sendRedirect("/shop/views/login.html");
        }else {
            //如果登录，放行
            filterChain.doFilter(req,resp);
        }
    }

    @Override
    public void destroy() {

    }
}
